<html>
<head><meta charset="utf-8"><title>Is the `if` in `else if` part of the first `if`? · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html">Is the `if` in `else if` part of the first `if`?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="229174290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229174290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229174290">(Mar 07 2021 at 09:53)</a>:</h4>
<p>Right now, the reference states:</p>
<blockquote>
<p>An <em>if expression</em> is a conditional branch in program control.<br>
The syntax of an if expression is an expression, called the <em>condition operand</em>, then a block expression, called the <em>consequent</em>, then any number of <code>else if</code> conditions and blocks, and an optional trailing <code>else</code> block.</p>
</blockquote>
<p>I want to change the syntax so that it's always a single optional else followed by an expression, called the <em>antecedent</em> and that it must be a block expression, an if expression, or an if let expression. Is that an okay change to make without lang team sign-off, or should I get official sign-off for this?</p>



<a name="229197318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229197318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229197318">(Mar 07 2021 at 15:52)</a>:</h4>
<p>It seems like your proposed change matches the grammar, so I would say it is fine.  I think that is also how rustc represents it in the AST.</p>
<p>I'm not sure about using the word "antecedent".  I've not seen that in PL texts. I'm guessing that comes from logic?  I'm not too familiar with that, but it seems strange to me. I've always seen it called the "else clause" or similar.</p>



<a name="229197345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229197345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229197345">(Mar 07 2021 at 15:52)</a>:</h4>
<p>Ditto about "antecedent", that doesn't match the usage in logic either</p>



<a name="229197352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229197352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229197352">(Mar 07 2021 at 15:53)</a>:</h4>
<p>the antecedent is the P in "P -&gt; Q"</p>



<a name="229197382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229197382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229197382">(Mar 07 2021 at 15:53)</a>:</h4>
<p>it's a real stretch to identify the else clause as that</p>



<a name="229197469"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229197469" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229197469">(Mar 07 2021 at 15:54)</a>:</h4>
<p>(the consequent is the Q in "P -&gt; Q", which seems more like the "then" clause than the "else" clause) EDIT: actually this matches the usage in the quote, I misread</p>



<a name="229197537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229197537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229197537">(Mar 07 2021 at 15:55)</a>:</h4>
<p>I mean it's literally read "if P then Q"</p>



<a name="229197610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229197610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229197610">(Mar 07 2021 at 15:56)</a>:</h4>
<p>If you want a term other than "else clause", "fallback clause" is an option, although that gives a kind of priority to the "then" clause</p>



<a name="229207427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229207427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229207427">(Mar 07 2021 at 18:26)</a>:</h4>
<p>Yeah, I would just call it the "else expression".</p>



<a name="229207435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229207435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229207435">(Mar 07 2021 at 18:26)</a>:</h4>
<p>That aside, this seems like a perfectly reasonable approach.</p>



<a name="229207471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229207471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229207471">(Mar 07 2021 at 18:27)</a>:</h4>
<p>I don't think there's anything special rust is doing to keep if/else-if chains together.</p>



<a name="229207477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229207477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229207477">(Mar 07 2021 at 18:27)</a>:</h4>
<p>So I don't know why the reference would need to reflect that.</p>



<a name="229207488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229207488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229207488">(Mar 07 2021 at 18:28)</a>:</h4>
<p>The reference should still explain the common pattern of if/else-if chaining, but doesn't need to treat it as a single grammatical construct.</p>



<a name="229209089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229209089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229209089">(Mar 07 2021 at 18:49)</a>:</h4>
<p>Well it is necessary to have if else if chains or something equivalent as part of the grammar since <code>if cond { e1 } else e2</code> is not valid but <code>if cond { e1 } else if cond2 { e2 }</code> is</p>



<a name="229209198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229209198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229209198">(Mar 07 2021 at 18:50)</a>:</h4>
<p>if the grammar only allowed the form <code>if cond { e1 } else { e2 }</code> then you would have to write <code>if cond { e1 } else { if cond2 { e2 } }</code></p>



<a name="229209302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229209302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229209302">(Mar 07 2021 at 18:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="296643">isHavvy</span> 's replacement text addresses this by restricting the expressions that can appear after an <code>else</code>; I don't think this particular nonterminal (block, if, or if-let) exists anywhere else in the grammar</p>



<a name="229210398"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229210398" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229210398">(Mar 07 2021 at 19:09)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> Ah, fair. We do need the special-case of "you can write an <code>if</code> without braces".</p>



<a name="229212858"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229212858" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229212858">(Mar 07 2021 at 19:47)</a>:</h4>
<p>That's as simple as saying the else expression is a block expression or another if (let) expression.</p>



<a name="229215025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Is%20the%20%60if%60%20in%20%60else%20if%60%20part%20of%20the%20first%20%60if%60%3F/near/229215025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Is.20the.20.60if.60.20in.20.60else.20if.60.20part.20of.20the.20first.20.60if.60.3F.html#229215025">(Mar 07 2021 at 20:20)</a>:</h4>
<p>Also, just checking Wikipedia, I've been using "antecedent" wrong for years... <a href="https://en.wikipedia.org/wiki/Antecedent_%28logic%29">https://en.wikipedia.org/wiki/Antecedent_%28logic%29</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>